# coding:utf8
"""
author:shadu@foxmail.com
"""
from bs4 import BeautifulSoup

from core.http_content import get_html_safely
from core.proxyqueue import ProxyList
from core.network import is_proxy_ip_and_port
from zeander.zeander import ZLOG

__version__ = '1.0'

SOURCE = 'VIP-Proxy'
URL = 'http://47.91.139.92:8080/5bbdaa4eaf4dda10'


def save(proxy=None):
    if proxy and isinstance(proxy, str):
        ProxyList.put(proxy)


def parse(html):
    proxy_count = 0
    proxies = eval(html)
    for proxy in proxies:
        if is_proxy_ip_and_port(proxy):
            save(str(proxy))
            proxy_count += 1
    ZLOG.info('Got %d proxies from %s' % (proxy_count, SOURCE))


def collect():
    html = get_html_safely(URL)
    if html == '-':
        ZLOG.alert('Failed to get HTML from %s' % URL)
    else:
        parse(html)


if __name__ == '__main__':
    collect()
